#include "PrecompiledHeaders.h"
#include "SkyBox.h"
#include "Constants.h"

#include <stdlib.h>
#include <time.h>

/*
 * Constructor
 */
SkyBox::SkyBox(GameResources *r)
{
    resources = r;
    loadConfig();
}

/*
 * Pick a (random?) skybox from the config list
 */
void SkyBox::loadConfig()
{
    Ogre::ConfigFile cf;
    std::string filename = Constants::RESOURCES_FOLDER + "materials/skyboxes/sky_boxes.ini";
    //printf("%s\n",filename.c_str());
    cf.load(filename);
    Ogre::ConfigFile::SectionIterator sec_i = cf.getSectionIterator();
    skies = std::vector<SkyData>(0);
    std::string section, name, value;
    // for each section
    while(sec_i.hasMoreElements()) {
        section = sec_i.peekNextKey();
        Ogre::ConfigFile::SettingsMultiMap *settings = sec_i.getNext();
        Ogre::ConfigFile::SettingsMultiMap::iterator elem_i;
        // for each attribute
        SkyData data;
        for (elem_i = settings->begin(); elem_i != settings->end(); elem_i++) {
            name = elem_i->first;
            value = elem_i->second;
            if(name == "material") {
                data.material = value;
            }
            if(name == "time") {
                data.time = getTimeOfDayFromString(value);
            }
        }
        if(name!="") {
            skies.push_back(data);
        }
    }
}

/*
 * Return sky box material
 */
std::string SkyBox::getSkyBox()
{
    //printf("TIME OF DAY = %i\n", getTimeOfDay());
    if (resources->config->getSkyBoxMode())
    {   
        srand((unsigned)time(0));
        return skies[rand() % (int)skies.size()].material;
    }
    else
    {
        return skies[getSkyIndexByTime()].material;
    }
    throw new Ogre::Exception(101, "Invalid time of day", "SkyBox::getSkyBox");
    return NULL;
}

/*
 * Return the index in the vector by the time of day
 */
int SkyBox::getSkyIndexByTime()
{
    TimeOfDay tod = getTimeOfDay();
    std::vector<int> matches(0);
    
    // search through the vector and note all skies matching tod
    for(unsigned int i=0; i<skies.size(); i++) {
        if(skies[i].time == tod) {
            matches.push_back(i);
        }
    }
    
    // randomly select one of the matches and return
    srand(time(NULL));
    return matches[rand() % (int)(matches.size())];
}

/*
 * Convert time of day to enumerated type
 */
TimeOfDay SkyBox::getTimeOfDayFromString(std::string time)
{
    if(time == "morning")   return morning;
    if(time == "afternoon") return afternoon;
    if(time == "evening")   return evening;
    if(time == "night")     return night;
    throw new Ogre::Exception(101, "Invalid time of day", "SkyBox::getTimeOfDayFromString");
    return morning;
}

/*
 * Return time of day from actual time
 */
TimeOfDay SkyBox::getTimeOfDay()
{
    time_t rawtime;
    struct tm *timeinfo;
    char buffer[6];

    time(&rawtime);
    timeinfo = localtime (&rawtime);

    strftime(buffer, 6, "%H",timeinfo);
    int hour = atoi(buffer);
    
    if(hour >= 6 && hour < 12)  return morning;
    if(hour >= 12 && hour < 17) return afternoon;
    if(hour >= 17 && hour < 21) return evening;
    return night;
}

